package ui.widget.card;

import java.util.List;

import ui.activity.card.CardListActivity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import business.entity.Card;

import com.lab107team.R;

public class CardListAdapter extends BaseAdapter {

	private CardListActivity activity;
	private List<Card> cardsList;
	private LayoutInflater layoutInflater;

	private static final int[] HEADS = { R.drawable.head_1, R.drawable.head_2,
			R.drawable.head_3, R.drawable.head_4, R.drawable.head_5 };

	public CardListAdapter(CardListActivity activity) {
		this.activity = activity;

		layoutInflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		this.cardsList = this.activity.getModel().getCardList();
		return cardsList != null ? cardsList.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		this.cardsList = this.activity.getModel().getCardList();
		return cardsList != null ? cardsList.get(position) : null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		this.cardsList = this.activity.getModel().getCardList();

		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.card_list_item, null);
		}

		if (cardsList != null) {
			String name = cardsList.get(position).getName();
			((TextView) convertView.findViewById(R.id.tv_card_list_item_name))
					.setText(name);
			int index = position % HEADS.length;
			int resId = HEADS[index];
			convertView.findViewById(R.id.iv_card_list_item_head)
					.setBackgroundResource(resId);
		}

		return convertView;
	}

}
